package com.wfit.bankproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfit.bankproject.entity.JsonResult;
import com.wfit.bankproject.entity.Role;
import com.wfit.bankproject.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {


    @Autowired
    private RoleService service;

    @PostMapping
    public JsonResult add(@RequestBody Role user){
        boolean save = service.save(user);
        if(save)
        {
            return JsonResult.ok();
        }
        else
            return JsonResult.failure(500,"添加数据失败");
    }
    @PutMapping
    public JsonResult update(@RequestBody Role entity){
        if (entity.getId()==null){
            return JsonResult.failure(400,"Id值不能为空");
        }

        boolean b = service.updateById(entity);
        if (b){
            return JsonResult.ok();
        }
        return  JsonResult.failure(500,"更新失败");
    }
    @GetMapping("/{id}")
    public JsonResult getById(@PathVariable Long id){
        Role entity = service.getById(id);
        return  JsonResult.ok(entity);

    }

    @DeleteMapping("/{id}")
    public JsonResult delbyId(@PathVariable Long id){
        boolean b = service.removeById(id);
        if (b){
            return JsonResult.ok();
        }
        return JsonResult.failure(300,"删除失败");
    }
//    @GetMapping("/fenye")
//    public JsonResult getAll(@RequestBody RoleDto dto){
//        if (dto.getCurrent()==null){
//            dto.setCurrent(1);
//        }
//        if (dto.getPageSize()==null){
//            dto.setPageSize(2);
//        }
//        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
//        boolean name = StringUtils.hasText(dto.getName());
//
//       queryWrapper.like(name, Role::getName, dto.getName());
//
//       List<Role> list = service.list(queryWrapper);
//        IPage<Role> page=new Page<>(dto.getCurrent(),dto.getPageSize());
//        IPage<Role> page1 = service.page(page, queryWrapper);
//        return JsonResult.ok(page1);
//
//    }

}
